/**
 * @class   wosh::BusConnector
 * @brief   Connect a IBusListener to a Bus, like a proxy.
 *
 ****************************************************************************
 * @version $Id: BusConnector.h 2873 2010-08-08 19:06:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_BusConnector_H__
 #define __WOSH_Core_BusConnector_H__

 #include <woshDefs.h>
 #include <core/Bus.h>
 #include <core/Reflection.h>


namespace wosh {


class BusConnector :	public virtual IBusListener,
						public virtual IReflection
{
	WOSH_CLASSNAME(wosh::BusConnector)
	WOSH_CLASS_ABSTRACT(false)

	public:
		/**
		 * @brief  Empty constructor.
		 */
		BusConnector();
		/**
		 * @brief  Empty destructor. Disconnect if Bus is still connected.
		 */
		virtual ~BusConnector();


/** @name management
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Connect to the requested Bus.
		 * @param [in] busname the name of the Bus to connected to
		 * @param [in] create_if_required when true, if the Bus is not found, it will be created
		 * @return WRET_ERR_ILLEGAL_USE when Bus is already connected; WRET_ERR_INTERNAL
		 *         on errors, as the Bus wasn't found; WRET_OK if Bus was connected.
		 * @see    BusManager::connectBus()
		 */
		virtual WRESULT connect( const std::string& busname, int connection_options = Bus::BUS_NO_OPTIONS );

		/**
		 * @brief  Disconnected the Bus.
		 * @return WRET_ERR_ILLEGAL_USE when Bus isn't connected; WRET_OK if Bus was disconnected.
		 * @see    BusManager::disconnectBus()
		 */
		virtual WRESULT disconnect( int connection_options = Bus::BUS_NO_OPTIONS );

		/**
		 * @brief  Post a message on the Bus.
		 * @param [in] message the message to be injected on Bus, object is not
		 *             copied, instance will be deleted by Bus when all handlers
		 *             received the message.
		 * @return false when Bus isn't connected, true else
		 * @see    Bus::postMessage()
		 */
		virtual bool postMessage( Message* message ) const;

		//virtual Message* postMessageWaitReply( Message* message, long timeout );

//@}
/** @name IBusListener Interface
 * @{
 ******************************************************************************/
	public:
		virtual void busMessage( const Message& message, const Bus* source );

		virtual void busEvent( Bus::BUS_STATE state, const Bus* source );

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Evaluate if Bus is connected. Basically checks if Bus is set (linked).
		 * @return true when Bus is connected, false else
		 */
		virtual bool isConnected() const;

		/**
		 * @brief  Get the name of the Bus.
		 * @return empty string when Bus is not connected (set), string containing the name else
		 * @see    Bus::getName()
		 */
		std::string getBusName() const;

		/**
		 * @brief  Get the Message Handler.
		 * @return the current message handler. May be NULL.
		 */
		IBusListener* getMessageHandler()			{ return this->handler; }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Set the Message Handler, it will be notified for new messages.
		 * @param [in] handler the handler to be set (overwritten). May be NULL.
		 */
		void setMessageHandler( IBusListener* handler );

		void setBus( Bus* bus_ptr )					{ this->bus = bus_ptr; }

//@}
	protected:
		Bus* bus;				///< the Bus to be connected
		IBusListener* handler;	///< handler to proxy the message

		friend class wosh::Bus;


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_BusConnector_H__
